﻿CREATE PROCEDURE [ServiceEngine].[DeleteSchemaObject]
@schemaObjectID UNIQUEIDENTIFIER
AS
BEGIN
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
	DECLARE @entityID UNIQUEIDENTIFIER;
	
	SET NOCOUNT ON;

	BEGIN TRY
		BEGIN
			BEGIN TRANSACTION
				DELETE 
					[SchemaObjectDependencies]
				WHERE 
					[SchemaObjectID] = @schemaObjectID OR
					[ParentObjectID] = @schemaObjectID
					
				DELETE
					[SchemaObjects]
				WHERE
					[SchemaObjectID] = @schemaObjectID 
					
			COMMIT TRANSACTION
		END
	END TRY
	BEGIN CATCH
		SELECT 
			@ErrorMessage = ERROR_MESSAGE(),
			@ErrorSeverity = ERROR_SEVERITY(),
			@ErrorState = ERROR_STATE();

		IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

		RAISERROR (@ErrorMessage, 
				   @ErrorSeverity,
				   1);
	END CATCH
END

